<?php

/**
 * This is the model class for table "lab_user".
 *
 * The followings are the available columns in table 'lab_user':
 * @property integer $user_id
 * @property string $PID
 * @property string $account
 * @property string $password
 * @property integer $role_id
 * @property string $name
 * @property string $tel
 *
 * The followings are the available model relations:
 * @property LabAppointment[] $labAppointments
 * @property LabLog[] $labLogs
 * @property LabRoom[] $labRooms
 * @property LabSchedule[] $labSchedules
 * @property LabRole $role
 * @property LabUserCourse[] $labUserCourses
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lab_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('PID, account, password, role_id, name, tel', 'required'),
			array('role_id', 'numerical', 'integerOnly'=>true),
			array('PID, name', 'length', 'max'=>40),
			array('account, password', 'length', 'max'=>255),
			array('tel', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, PID, account, password, role_id, name, tel', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'Appointments' => array(self::HAS_MANY, 'Appointment', 'user_id'),
			'Logs' => array(self::HAS_MANY, 'Log', 'user_id'),
			'Rooms' => array(self::HAS_MANY, 'Room', 'admin_user_id'),
			'Schedules' => array(self::HAS_MANY, 'Schedule', 'user_id'),
			'Role' => array(self::BELONGS_TO, 'Role', 'role_id'),
			'Courses' => array(self::MANY_MANY, 'Course', 'lab_user_course(user_id,course_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'PID' => 'Pid',
			'account' => 'Account',
			'password' => 'Password',
			'role_id' => 'Role',
			'name' => 'Name',
			'tel' => 'Tel',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('PID',$this->PID,true);
		$criteria->compare('account',$this->account,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('tel',$this->tel,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function validatePassword($password)
	{
		return strtoupper(md5($password))===$this->password;
	}
}